/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> pathTarget(TreeNode* root, int target) {
        recur(root,target);
        return res;
    }
private:
    vector<vector<int>>res;
    vector<int>path;

    //先序遍历
    void recur(TreeNode* root,int target)
    {
        if(root==nullptr)return;
        path.push_back(root->val);//入
        target-=root->val;

        if(target==0&&root->left==nullptr&&root->right==nullptr)
        {
            res.push_back(path);
        }
        recur(root->left,target);
        recur(root->right,target);

        path.pop_back();//出，回退
    }
};